package dLogistic.domain.service;

import dLogistic.application.service.PaczkaService;
import dLogistic.domain.model.Magazyn.MagazynAggr;
import dLogistic.domain.model.Paczka.TrasaPaczki;
import dLogistic.infrastructure.persistence.hibernate.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.List;

/**
 * Created by Przemek on 2014-10-26.
 */
public class MagazynService {
    protected static SessionFactory factory;

    public void PaczkiDlaKuriera(String uuid_magazyn, String uuid_kurier){
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();

        Transaction transaction = session.beginTransaction();
        String sql = "select * from TrasaPaczki where uuid_kurier = :uuid_kurier and RoadStart = :uuid_magazyn and DataStart = \"\"";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(TrasaPaczki.class);
        query.setParameter("uuid_kurier", uuid_kurier);
        query.setParameter("uuid_magazyn", uuid_magazyn);
        List results = query.list();

        transaction.rollback();
        PaczkaService paczkaService = new PaczkaService();
        for (Integer i = 0; i < results.size(); i++){
            TrasaPaczki trasaPaczki = (TrasaPaczki) results.get(i);
            paczkaService.przekazaniePaczki(trasaPaczki.uuid_paczka(), uuid_magazyn, uuid_kurier,0);
        }

    }

    public void nowaDrogaDoMagazynu(MagazynAggr magazynOd, MagazynAggr magazynDo, Long droga){


    }
}
